<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FeedController
 *
 * @author QuangKhoi
 */
class SignalController {
    /**
     * Store model object
     * @var Signal
     */
    private $model;
    
    /**
     * Store view for the stream
     * @var SignalView
     */
    private $view;
    
    /**
     * 
     * @param type $model
     * @param type $view
     */
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }
    
    /**
     * Update folder infos or add new signal to stream
     */
    public function save() {
        $model = $this->model;
        if ($model->signalId) { //OK
            Database::getInstance()->query("UPDATE signals
                    SET messageid='$model->messageId', 
                    userid='$model->userID', 
                    streamid='$model->streamID', 
                    signaltype='$model->signalType',
                    date='$model->date',
                    gender='$model->gender',
                    tag='$model->tag',
                    sentiment='$model->sentiment',
                    sharecount='$model->shareCount' 
                    WHERE messageId = $model->messageId");
            return true;
        }
        else {
            $dt = new DateTime($model->date);
            $date = $dt->format("Y-m-d H:i:s");
            $model->signalID = Database::getID("signals", "signalid");
            Database::getInstance()->query("INSERT INTO signals
                        (signalid, messageid, userid, streamid, signaltype, date, gender,
                         tag, sentiment, sharecount) VALUES
                        ('$model->signalID', '$model->messageId', '$model->userID', 
                        '$model->streamID', '$model->signalType', 
                        '$date', '$model->gender', 
                        '$model->tag', '$model->sentiment',
                        '$model->shareCount')");
            return true;
        }
    }
    
    /**
     * Delete this stream
     */
    public function delete() {
        $model = $this->model;
        $query = "DELETE FROM signals WHERE signalid = $model->signalId";
        Database::getInstance()->query($query);
        return true;
    }
}

?>
